package jmine.tec.proxy;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

import jmine.tec.proxy.chain.ChainElement;
import jmine.tec.proxy.chain.InvocationChain;

/**
 * Silly {@link ChainElement} that counts the number of times its called.
 * 
 * @author takeshi
 * 
 */
public final class InvocationCountChainElement implements ChainElement, Serializable {

    private static final long serialVersionUID = 7387112446373118971L;

    private final AtomicInteger count = new AtomicInteger();

    /**
     * @param chain
     *            chain
     * @return object
     * @throws Throwable
     *             t
     * @see br.com.maps.util.proxy.chain.ChainElement#aroundInvoke(br.com.maps.util.proxy.chain.InvocationChain)
     */
    public Object aroundInvoke(final InvocationChain chain) throws Throwable {
        count.incrementAndGet();
        return chain.proceed();
    }

    /**
     * 
     * @return int
     */
    public int count() {
        return count.get();
    }

}
